package com.sgb.kjwl.view.ui.adapter.self;

/**
 * Created by harisucici on 2020/5/15
 * Class description:
 */

import android.app.Activity;
import android.content.Context;
import android.view.View;

import androidx.databinding.ViewDataBinding;

import com.harisucici.jetpackbase.view.HBaseAdapter;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemAddressUserBinding;
import com.sgb.kjwl.model.entity.user.UserAddressEntity;

public class HUserAddressAdapter extends HBaseAdapter<UserAddressEntity, ItemAddressUserBinding> {
    private BindingListenConfirm confirm;
    private BindingListenConfirm editClick;
    private BindingListenConfirm cancleClick;
    private Context context;
    private ItemClickCallBack itemCallBack;

    public HUserAddressAdapter(Activity context, BindingListenConfirm<UserAddressEntity, ItemAddressUserBinding> confirm, BindingListenConfirm<UserAddressEntity, ItemAddressUserBinding> editClick, BindingListenConfirm<UserAddressEntity, ItemAddressUserBinding> cancleClick) {
        super(context);
        this.context = context;
        this.confirm = confirm;
        this.editClick = editClick;
        this.cancleClick = cancleClick;
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_address_user;
    }

    @Override
    protected void onBindItem(final ItemAddressUserBinding binding, final UserAddressEntity entity, final int position) {

        binding.radioIsDef.setOnClickListener((v) -> {
            if (entity.isDefaultAdress()) {
                return;
            }
            confirm.onItemClick(binding, entity, position);
        });

        binding.tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClick.onItemClick(binding, entity, position);
            }
        });

        binding.tvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cancleClick.onItemClick(binding, entity, position);
            }
        });

        binding.rlContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != itemCallBack) {
                    itemCallBack.onItemClick(entity);
                }
            }
        });

        binding.setViewModel(entity);
        binding.executePendingBindings();
    }

    @FunctionalInterface
    public interface BindingListenConfirm<T, B extends ViewDataBinding> {
        void onItemClick(B binding, T t, int i);
    }


    public void setItemClickCallBack(ItemClickCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    public interface ItemClickCallBack {
        void onItemClick(UserAddressEntity entity);
    }

}